package com.wondertek.service.sdk.iotdh.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.wondertek.common.base.Result;
import com.wondertek.common.exception.AppException;
import com.wondertek.common.util.base.IpUtils;
import com.wondertek.service.entity.business.SdkErrorRecordPo;
import com.wondertek.service.mapper.business.SdkErrorRecordMapper;
import com.wondertek.service.sdk.iotdh.config.SdkConfig;
import com.wondertek.service.sdk.iotdh.constant.DeviceDhConstant;
import com.wondertek.service.sdk.iotdh.dto.req.*;
import com.wondertek.service.sdk.iotdh.dto.req.arcConfig.AddArcConfigReq;
import com.wondertek.service.sdk.iotdh.dto.req.installer.AddCameraReq;
import com.wondertek.service.sdk.iotdh.dto.req.installer.GetSearchIpcResultReq;
import com.wondertek.service.sdk.iotdh.dto.req.installer.SearchIpcStartingReq;
import com.wondertek.service.sdk.iotdh.dto.req.installer.StopSearchIpcReq;
import com.wondertek.service.sdk.iotdh.dto.req.network.SetMobileNetworkReq;
import com.wondertek.service.sdk.iotdh.dto.req.scene.AddAccessorySceneModeReq;
import com.wondertek.service.sdk.iotdh.dto.req.scene.GetAccessorySceneModesReq;
import com.wondertek.service.sdk.iotdh.dto.req.utc.SetDeviceUTCTimezoneReq;
import com.wondertek.service.sdk.iotdh.dto.res.*;
import com.wondertek.service.sdk.iotdh.dto.res.arcConfig.GetArcConfigRes;
import com.wondertek.service.sdk.iotdh.dto.res.deviceDetail.DeviceDetailRes;
import com.wondertek.service.sdk.iotdh.dto.res.deviceDetail.ListDeviceDetailsByIdsRes;
import com.wondertek.service.sdk.iotdh.dto.res.installer.AddCameraRes;
import com.wondertek.service.sdk.iotdh.dto.res.installer.ComIdRes;
import com.wondertek.service.sdk.iotdh.dto.res.installer.DeviceOnlineRes;
import com.wondertek.service.sdk.iotdh.dto.res.installer.GetSearchIpcResultRes;
import com.wondertek.service.sdk.iotdh.dto.res.network.*;
import com.wondertek.service.sdk.iotdh.dto.res.pir.SetPIRAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.alarmBell.SetODAlarmBellAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.doorDetector.SetDoorDetectorAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.doorDetectorPlus.SetDoorDetectorPlusAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.keyboard.SetKeyboardAccessoryInfoReq;
import com.wondertek.service.sdk.iotdh.dto.res.scene.GetAccessorySceneModesRes;
import com.wondertek.service.sdk.iotdh.dto.res.test.SensitivityTestReq;
import com.wondertek.service.sdk.iotdh.dto.res.test.SignalStrengthTestReq;
import com.wondertek.service.sdk.iotdh.dto.res.utc.GetDevUTCTimezoneRes;
import com.wondertek.service.sdk.iotdh.service.DhDeviceService;
import com.wondertek.service.sdk.iotdh.service.DhSdkService;
import com.wondertek.service.sdk.iotdh.util.DeviceUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class DhDeviceServiceImpl implements DhDeviceService {

    private static final Logger log = LoggerFactory.getLogger(DhDeviceServiceImpl.class);
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private SdkConfig sdkConfig;

    @Resource
    private DeviceUtil deviceUtil;

    @Resource
    private SdkErrorRecordMapper sdkErrorRecordMapper;

    @Resource
    private DhSdkService dhSdkService;


    @Override
    public Result<DeviceAddDhRes> addDevice(DeviceDhAddReq req) {
        String orgId = dhSdkService.listDept(new DeptListReq());
        req.setOrgId(orgId);
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.ADD_DEVICE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台addDevice入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台addDevice接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                //设置心跳时间
                SetHeartbeatReq heartbeatReq = new SetHeartbeatReq();
                heartbeatReq.setDeviceId(req.getDeviceSn());
                heartbeatReq.setInterval(20);
                heartbeatReq.setOfflineTimes(5);

                try {
                    setHeartbeat(heartbeatReq);
                } catch (Exception e) {
                    log.info("设置心跳时间失败！");
                }

                try {
                    SetDeviceUTCTimezoneReq setDeviceUTCTimezoneReq = new SetDeviceUTCTimezoneReq();
                    setDeviceUTCTimezoneReq.setDeviceId(req.getDeviceSn());
                    setDeviceUTCTimezoneReq.setTimezone(8);
                    setDeviceUTCTimezoneReq.setAreaDesc("Chennai");
                    this.setDeviceUTCTimezone(setDeviceUTCTimezoneReq);
                } catch (Exception e) {
                    log.info("设置时区失败！");
                }
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return JSONObject.parseObject(exchange.getBody(), Result.class);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> deleteDevice(DeviceDhDeleteReq req) {

        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.DELETE_DEVICE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台deleteDevice接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台deleteDevice接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (Exception e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<DevicePageRes> pageDevice(DeviceListReq req) {

        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.PAGE_DEVICE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台pageDevice接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台pageDevice接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return JSONObject.parseObject(exchange.getBody(), Result.class);
            }
        } catch (Exception e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }

    }

    @Override
    public Result<String> addStreamUrl(StreamUrlAddDhReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.ADD_STREAM_URL);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台addStreamUrl接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台addStreamUrl接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return JSONObject.parseObject(exchange.getBody(), Result.class);
            }
        } catch (Exception e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> operate(OperateDhReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.OPERATE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台operate接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台operate接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (Exception e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setTimingArmInfos(SetTimingArmInfosReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_TIMING_ARM_INFOS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setTimingArmInfos接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setTimingArmInfos接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (Exception e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetTimingArmInfosRes> getTimingArmInfos(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_TIMING_ARM_INFOS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getTimingArmInfos接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getTimingArmInfos接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object dataObj = jsonObject.get("data");
                if (ObjectUtil.isEmpty(dataObj)) {
                    return Result.success(new GetTimingArmInfosRes());
                }
                GetTimingArmInfosRes getTimingArmInfosRes = JSONObject.parseObject(JSON.toJSONString(dataObj), GetTimingArmInfosRes.class);
                return Result.success(getTimingArmInfosRes);
            }
        } catch (Exception e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<PageAlarmRes> pageAlarm(AlarmListDhReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.PAGE_ALARM);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台pageAlarm接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台pageAlarm接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object dataObj = jsonObject.get("data");
                if (ObjectUtil.isEmpty(dataObj)) {
                    return Result.success(new PageAlarmRes());
                }
                PageAlarmRes pageAlarmRes = JSONObject.parseObject(JSON.toJSONString(dataObj), PageAlarmRes.class);
                return Result.success(pageAlarmRes);
            }
        } catch (RestClientException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> subscribeDatas(SubscribeDatasDhReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SUBSCRIBE_DATAS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台subscribeDatas接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台subscribeDatas接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }

    }

    @Override
    public Result<?> subscribeCancel(SubscribeCancelDhReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SUBSCRIBE_CANCEL);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台subscribeCancel接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台subscribeCancel接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (Exception e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }


    @Override
    public Result<?> setAreaInfo(AetAreaInfoReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_AREA_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setAreaInfo接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setAreaInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (Exception e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<SetAreaArmModeRes> setAreaArmMode(SetAreaArmModeReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_AREA_ARM_MODE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setAreaArmMode接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setAreaArmMode接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object dataObj = jsonObject.get("data");
                if (ObjectUtil.isEmpty(dataObj)) {
                    return Result.success(new SetAreaArmModeRes());
                }
                SetAreaArmModeRes setAreaArmModeRes = JSONObject.parseObject(JSON.toJSONString(dataObj), SetAreaArmModeRes.class);
                return Result.success(setAreaArmModeRes);
            }
        } catch (Exception e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetAreaInfosRes> getAreaInfos(GetAreaInfosReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_AREA_INFOS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getAreaInfos接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getAreaInfos接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return JSONObject.parseObject(exchange.getBody(), Result.class);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetAreaArmModeRes> getAreaArmMode(GetAreaInfosReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_AREA_ARM_MODE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getAreaArmMode接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getAreaArmMode接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return JSONObject.parseObject(exchange.getBody(), Result.class);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setAccessoryPair(SetAccessoryPairReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_ACCESSORY_PAIR);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setAccessoryPair接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setAccessoryPair接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setAccessoryArea(SetAccessoryAreaReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_ACCESSORY_AREA);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setAccessoryArea接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setAccessoryArea接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<List<GetAccessoryInfoRes>> getAccessoryInfo(GetAccessoryInfoReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_ACCESSORY_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getAccessoryInfo接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getAccessoryInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                ArrayList<GetAccessoryInfoRes> accessoryAreaRes = new ArrayList<>();
                Object data = jsonObject.get("data");
                if (data != null) {
                    JSONObject jsonData = JSONObject.parseObject(data.toString());
                    JSONArray infos = jsonData.getJSONArray("accessoryInfos");
                    if (CollectionUtil.isNotEmpty(infos)) {
                        for (Object o : infos) {
                            GetAccessoryInfoRes setAccessoryAreaRes = JSONObject.parseObject(o.toString(), GetAccessoryInfoRes.class);
                            accessoryAreaRes.add(setAccessoryAreaRes);
                        }
                    }
                }
                return Result.success(accessoryAreaRes);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> deleteAccessory(DeleteAccessoryReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.DELETE_ACCESSORY);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台deleteAccessory接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台deleteAccessory接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }


    @Override
    public Result<GetStatusRes> getStatus(GetStatusReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_STATUS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getStatus接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());

            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getStatus接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                JSONObject data = (JSONObject) jsonObject.get("data");
                if (data != null) {
                    Object deviceStatus = data.get("deviceStatus");
                    GetStatusRes getStatusRes = JSONObject.parseObject(deviceStatus.toString(), GetStatusRes.class);
                    return Result.success(getStatusRes);
                }
                return Result.success(new GetStatusRes());
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<Boolean> getEnable(GetEnableReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_ENABLE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getEnable接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);

            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getEnable接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.getBoolean("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setEnable(SetEnableReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_ENABLE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setEnable接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setEnable接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setSmokeAccessoryInfo(SetSmokeAccessoryInfoReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_SMOKE_ACCESSORY_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setSmokeAccessoryInfo接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setSmokeAccessoryInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setNetworkInfo(SetNetworkInfoReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_NET_WORK_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setNetworkInfo接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setNetworkInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> modifyWirelessNetwork(ModifyWirelessNetwork req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.MODIFY_WIRELESS_NETWORK);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台modifyWirelessNetwork接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台modifyWirelessNetwork接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setKeyboardAccessoryInfo(SetKeyboardAccessoryInfoReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_KEYBOARD_ACCESSORY_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setKeyboardAccessoryInfo接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setKeyboardAccessoryInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setODAlarmBellAccessoryInfo(SetODAlarmBellAccessoryInfoReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_OD_ALARM_BELL_ACCESSORY_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setODAlarmBellAccessoryInfo接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setODAlarmBellAccessoryInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setDoorDetectorAccessoryInfo(SetDoorDetectorAccessoryInfoReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_DOOR_DETECTOR_ACCESSORY_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setDoorDetectorAccessoryInfo接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setDoorDetectorAccessoryInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setDoorDetectorPlusAccessoryInfo(SetDoorDetectorPlusAccessoryInfoReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_DOOR_DETECTOR_PLUS_ACCESSORY_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setDoorDetectorPlusAccessoryInfo接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setDoorDetectorPlusAccessoryInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setPIRAccessoryInfo(SetPIRAccessoryInfoReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_PIR_ACCESSORY_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setPIRAccessoryInfo接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setPIRAccessoryInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> sensitivityTest(SensitivityTestReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SENSITIVITY_TEST);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台sensitivityTest接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台sensitivityTest接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> signalStrengthTest(SignalStrengthTestReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SIGNAL_STRENGTH_TEST);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台signalStrengthTest接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台signalStrengthTest接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<List<LocalRecordRes>> LocalRecords(LocalRecordsReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.LOCAL_RECORDS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台LocalRecords接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台LocalRecords接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                JSONObject jObj = (JSONObject) jsonObject.get("data");
                if (ObjectUtil.isEmpty(jObj)) {
                    return Result.success(new ArrayList<>());
                }
                JSONArray jsonArray = jObj.getJSONArray("records");
                if (CollectionUtil.isEmpty(jsonArray)) {
                    return Result.success(new ArrayList<>());
                }
                Gson gson = new Gson();
                Type listType = new TypeToken<List<LocalRecordRes>>() {
                }.getType();
                List<LocalRecordRes> localRecordRes = gson.fromJson(JSON.toJSONString(jsonArray), listType);
                return Result.success(localRecordRes);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<CheckDeviceBindInfoRes> checkDeviceBindInfo(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.CHECK_DEVICE_BIND_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台checkDeviceBindInfo接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台checkDeviceBindInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object dataObj = jsonObject.get("data");
                if (ObjectUtil.isEmpty(dataObj)) {
                    return Result.success(new CheckDeviceBindInfoRes());
                }
                CheckDeviceBindInfoRes res = JSONObject.parseObject(JSON.toJSONString(dataObj), CheckDeviceBindInfoRes.class);
                return Result.success(res);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<ComIdRes> getIpcInstance(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_IPC_INSTANCE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getIpcInstance接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getIpcInstance接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                JSONObject dataObj = (JSONObject) jsonObject.get("data");
                if (ObjectUtil.isEmpty(dataObj)) {
                    return Result.success(new ComIdRes());
                }
                return Result.success(JSONObject.parseObject(JSON.toJSONString(dataObj), ComIdRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> searchIpcStarting(SearchIpcStartingReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SEARCH_IPC_STARTING);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台searchIpcStarting接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台searchIpcStarting接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.getString("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<List<GetSearchIpcResultRes>> getSearchIpcResult(GetSearchIpcResultReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_SEARCH_IPC_RESULT);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getSearchIpcResult接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getSearchIpcResult接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                if (CollectionUtil.isEmpty(jsonArray)) {
                    return Result.success(new ArrayList<>());
                }
                Gson gson = new Gson();
                Type listType = new TypeToken<List<GetSearchIpcResultRes>>() {
                }.getType();
                List<GetSearchIpcResultRes> resultRes = gson.fromJson(JSON.toJSONString(jsonArray), listType);
                return Result.success(resultRes);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> stopSearchIpc(StopSearchIpcReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.STOP_SEARCH_IPC);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台stopSearchIpc接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台stopSearchIpc接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.getString("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<AddCameraRes> addCamera(AddCameraReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.ADD_CAMERA);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台addCamera接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台addCamera接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object dataObj = jsonObject.get("data");
                AddCameraRes addCameraRes = JSONObject.parseObject(JSON.toJSONString(dataObj), AddCameraRes.class);
                return Result.success(addCameraRes);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setDeviceUTCTimezone(SetDeviceUTCTimezoneReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_DEVICE_UTC_TIMEZONE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setDeviceUTCTimezone接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setDeviceUTCTimezone接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.getString("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetDevUTCTimezoneRes> getDevUTCTimezone(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_DEVICE_UTC_TIMEZONE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getDevUTCTimezone接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getDevUTCTimezone接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object dataObj = jsonObject.get("data");
                return Result.success(JSONObject.parseObject(JSON.toJSONString(dataObj), GetDevUTCTimezoneRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }


    @Override
    public Result<?> setHeartbeat(SetHeartbeatReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_HEARTBEAT);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台setHeartbeat接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setHeartbeat接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object dataObj = jsonObject.get("data");
                return Result.success(JSONObject.parseObject(JSON.toJSONString(dataObj), GetDevUTCTimezoneRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetAccessorySceneModesRes> getAccessorySceneModes(GetAccessorySceneModesReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_ACCESSORY_SCENE_MODES);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getAccessorySceneModes接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getAccessorySceneModes接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object dataObj = jsonObject.get("data");
                return Result.success(JSONObject.parseObject(JSON.toJSONString(dataObj), GetAccessorySceneModesRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> addAccessorySceneMode(AddAccessorySceneModeReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_ACCESSORY_SCENE_MODES);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台addAccessorySceneMode接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台addAccessorySceneMode接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> addArcConfig(AddArcConfigReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.ADD_ARC_CONFIG);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台addArcConfig接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台addArcConfig接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                return Result.success(jsonObject.get("data"));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetArcConfigRes> getArcConfig(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_ARC_CONFIG);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台getArcConfig接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getArcConfig接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object dataObj = jsonObject.get("data");
                return Result.success(JSONObject.parseObject(JSON.toJSONString(dataObj), GetArcConfigRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<DeviceOnlineRes> deviceOnline(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.DEVICE_ONLINE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            log.info("调用大华平台deviceOnline接口入参:{}", JSONObject.toJSONString(req));
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台deviceOnline接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(JSONObject.parseObject(JSON.toJSONString(data), DeviceOnlineRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> subscribeList() {
        HttpEntity<String> entity = getEntity(null);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SUBSCRIBE_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", null);
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.GET, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台subscribeList接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(data);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> modifyDevCode(ModifyDevCodeReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.MODIFY_DEV_CODE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台modifyDevCode接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(data);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> verifyDevCode(VerifyDevCodeReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.VERIFY_DEV_CODE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台verifyDevCode接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(data);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> upgradeDevice(VerifyDevCodeReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.UPGRADE_DEVICE);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台upgradeDevice接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(data);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setAbilityStatus(SetAbilityStatusReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_ABILITY_STATUS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setAbilityStatus接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(data);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<?> setMobileNetwork(SetMobileNetworkReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.SET_MOBILE_NETWORK);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台setMobileNetwork接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(data);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<ListDeviceDetailsByIdsRes> listDeviceDetailsByIds(ListDeviceDetailsByIdsReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.LIST_DEVICE_DETAILS_BY_IDS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台listDeviceDetailsByIds接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                ListDeviceDetailsByIdsRes idsRes = new ListDeviceDetailsByIdsRes();
                JSONObject obj = jsonObject.getJSONObject("data");
                if (obj == null) {
                    return Result.success(idsRes);
                }
                JSONArray jsonArray = obj.getJSONArray("deviceList");
                if (CollectionUtil.isEmpty(jsonArray)) {
                    return Result.success(idsRes);
                }

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DeviceDetailRes>>() {
                }.getType();
                List<DeviceDetailRes> resultRes = gson.fromJson(JSON.toJSONString(jsonArray), listType);
                idsRes.setDeviceList(resultRes);
                idsRes.setCount(jsonObject.getIntValue("count"));
                return Result.success(idsRes);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetNetworkInfoRes> getNetworkInfo(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_NETWORK_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getNetworkInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(JSONObject.parseObject(JSON.toJSONString(data), GetNetworkInfoRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetCurrentWirelessNetworkRes> getCurrentWirelessNetwork(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_CURRENT_WIRELESS_NETWORK);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getCurrentWirelessNetwork接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(JSONObject.parseObject(JSON.toJSONString(data), GetCurrentWirelessNetworkRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<List<GetMobileNetworkInfoRes>> getMobileNetworkInfo(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_MOBILE_NETWORK_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getMobileNetworkInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                Gson gson = new Gson();
                Type listType = new TypeToken<List<GetMobileNetworkInfoRes>>() {
                }.getType();
                List<GetMobileNetworkInfoRes> resultRes = gson.fromJson(JSON.toJSONString(jsonArray), listType);
                return Result.success(resultRes);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetAroundWirelessNetworkInfosRes> getAroundWirelessNetworkInfos(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_AROUND_WIRELESS_NETWORK_INFOS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getAroundWirelessNetworkInfos接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(JSONObject.parseObject(JSON.toJSONString(data), GetAroundWirelessNetworkInfosRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetUpgradeVersionInfoRes> getUpgradeVersionInfo(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_UPGRADE_VERSION_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getUpgradeVersionInfo接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(JSONObject.parseObject(JSON.toJSONString(data), GetUpgradeVersionInfoRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<GetUpgradeProgressRes> getUpgradeProgress(ComId req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_UPGRADE_PROGRESS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getUpgradeProgress接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                Object data = jsonObject.get("data");
                return Result.success(JSONObject.parseObject(JSON.toJSONString(data), GetUpgradeProgressRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<List<GetAccessoryStatusRes>> getAccessoryStatus(GetAccessoryInfoReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.GET_ACCESSORY_STATUS);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台getAccessoryStatus接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                JSONObject jsObj = jsonObject.getJSONObject("data");
                if (null == jsObj) {
                    return Result.success(new ArrayList<>());
                }

                JSONArray jsonArray = jsObj.getJSONArray("accessoryStatus");
                if (CollectionUtil.isEmpty(jsonArray)) {
                    return Result.success(new ArrayList<>());
                }
                Gson gson = new Gson();
                Type listType = new TypeToken<List<GetAccessoryStatusRes>>() {
                }.getType();
                List<GetAccessoryStatusRes> resultRes = gson.fromJson(JSON.toJSONString(jsonArray), listType);
                return Result.success(resultRes);
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }

    @Override
    public Result<DeviceVersionListRes> deviceVersionList(DeviceVersionListReq req) {
        HttpEntity<String> entity = getEntity(req);
        String url = sdkConfig.getBaseUrl().concat(DeviceDhConstant.DEVICE_VERSION_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("sdkAddress", url);
        map.put("inputParameters", JSONObject.toJSONString(req));
        try {
            ResponseEntity<String> exchange = restTemplate.exchange(url,
                    HttpMethod.POST, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            assert jsonObject != null;
            if (jsonObject.getIntValue("code") != 0) {
                log.info("调用大华平台deviceVersionList接口错误:{}", jsonObject.toJSONString());
                throw new AppException(jsonObject.toJSONString());
            } else {
                map.put("outputParameters", exchange.getBody());
                saveLog(map);
                JSONObject jsObj = jsonObject.getJSONObject("data");
                if (jsObj == null) {
                    return Result.success(new DeviceVersionListRes());
                }
                return Result.success(JSONObject.parseObject(JSON.toJSONString(jsObj), DeviceVersionListRes.class));
            }
        } catch (RestClientException | AppException e) {
            map.put("outputParameters", e.getMessage());
            saveLog(map);
            throw new AppException(getMsgInfo(e));
        }
    }


    public HttpEntity<String> getEntity(Object req) {
        String token = deviceUtil.getToken();
        HttpHeaders headers = deviceUtil.getCommonHeaders(token);
        String jsonData = JSONObject.toJSONString(req);
        return new HttpEntity<>(jsonData, headers);
    }

    public String getMsgInfo(Exception e) {
        System.out.println(e.getMessage());
        String msg = "";
        try {
            String[] split = e.getMessage().split(":", 2);
            // 定义正则表达式来匹配msg字段
            String regex = "\"msg\":\"(.*?)\"";

            // 创建Pattern对象
            Pattern pattern = Pattern.compile(regex);

            // 创建Matcher对象
            Matcher matcher = pattern.matcher(split[1]);

            // 查找匹配项
            if (matcher.find()) {
                // 提取msg字段的值
                msg = matcher.group(1);

                // 输出msg的值
                System.out.println("msg: " + msg);
            } else {
                System.out.println("未找到msg字段");
            }
        } catch (Exception ex) {
        }

        return msg;
    }
    public void saveLog(Map<String, String> map) {
        try {
            CompletableFuture.runAsync(() -> {//异步处理
                //统一 记录一下调用第三方接口日志
                SdkErrorRecordPo errorRecordPo = new SdkErrorRecordPo();
                errorRecordPo.setIpAddress(IpUtils.getIpAddr());
                errorRecordPo.setSdkAddress(map.get("sdkAddress"));
                errorRecordPo.setInputParameters(map.get("inputParameters"));
                errorRecordPo.setErrorInfo(map.get("outputParameters"));
                sdkErrorRecordMapper.insert(errorRecordPo);
            }).join();
        } catch (Exception e) {
            log.info("错误日志记录失败！！！");
        }
    }
}
